package com.mxpio.erp.technology.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.technology.entity.BomInfoLineAudit;
import com.mxpio.erp.common.technology.service.BomInfoLineAuditService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "BomInfoLineAuditController", description = "Bom审核行接口")
@RestController
@RequestMapping("/erp/tech/bomline/audit")
public class BomInfoLineAuditController {
    @Autowired
    private BomInfoLineAuditService bomInfoLineAuditService;

    @GetMapping("list")
    @Operation(summary = "Bom行列表", description = "获取Bom行列表（无分页）", method = "GET")
    public Result<List<BomInfoLineAudit>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<BomInfoLineAudit> bomInfoLineList = bomInfoLineAuditService.list(BomInfoLineAudit.class, criteria);
        return Result.OK(bomInfoLineList);
    }

    @GetMapping("page")
    @Operation(summary = "Bom行列表", description = "获取Bom行列表（分页）", method = "GET")
    public Result<Page<BomInfoLineAudit>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<BomInfoLineAudit> bomInfoLineList = bomInfoLineAuditService.listPage(BomInfoLineAudit.class, page, criteria);
        return Result.OK(bomInfoLineList);
    }
    @GetMapping("list/{bomId}")
    @Operation(summary = "根据bomId获取Bom行", description = "根据bomId获取Bom行", method = "GET")
    public Result<List<BomInfoLineAudit>> getByBomId(@Parameter(description="bomId") @PathVariable(name = "bomId", required = true) String bomId) {
        Criteria c = Criteria.create().addCriterion("bomId", Operator.EQ, bomId);
        List<BomInfoLineAudit> bomInfoLineList = bomInfoLineAuditService.list(BomInfoLineAudit.class, c);
        return Result.OK(bomInfoLineList);
    }

    @GetMapping("list/{bomLineId}")
    @Operation(summary = "根据bomLineId获取Bom行", description = "根据bomLineId获取Bom行", method = "GET")
    public Result<BomInfoLineAudit> getById(@Parameter(description="bomLineId") @PathVariable(name = "bomLineId", required = true)String bomLineId) {
        BomInfoLineAudit bomInfoLine = bomInfoLineAuditService.getById(BomInfoLineAudit.class, bomLineId);
        return Result.OK(bomInfoLine);
    }

    @PostMapping("add")
    @Operation(summary = "新增Bom行", description = "新增Bom行", method = "POST")
    public Result<BomInfoLineAudit> add(@RequestBody BomInfoLineAudit bomInfoLine) {
        bomInfoLineAuditService.save(bomInfoLine);
        return Result.OK(bomInfoLine);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑Bom行", description = "编辑Bom行", method = "PUT")
    public Result<BomInfoLineAudit> edit(@RequestBody BomInfoLineAudit bomInfoLine) {
        bomInfoLineAuditService.update(bomInfoLine);
        return Result.OK(bomInfoLine);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除Bom行", description = "删除Bom行,多个id用英文逗号分隔", method = "DELETE")
    public Result<BomInfoLineAudit> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            bomInfoLineAuditService.remove(BomInfoLineAudit.class, key);
        }
        return Result.OK();
    }

}
